package com.fjnldx.mes.service.scheduling.alarm;

import com.fjnldx.mes.domain.pojo.alarm.AlarmConfig;
import com.fjnldx.mes.domain.pojo.alarm.AlarmNotice;
import com.fjnldx.mes.domain.pojo.alarm.AlarmRecord;
import com.fjnldx.mes.domain.pojo.alarm.AlarmRules;
import com.fjnldx.mes.service.scheduling.alarm.model.MonitorModel;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 告警监控
 */

@Service
public class AlarmSurveillance {

    @Resource
    private AlarmSchedulingService service;

    //触发告警
    @Scheduled(cron = "0 0/5 * * * ?")
    @Async
    public void deviceAlarm() throws Exception {
        //获取所有配置
        List<AlarmConfig> configList = service.GetConfigs();
        //告警标识集合
        List<Integer> configIds = configList.stream().map(AlarmConfig::getId).collect(Collectors.toList());
        if (configIds.size() == 0) return;
        //获取所有配置的规则
        List<AlarmRules> rules = service.GetConfigRules(configIds);
        //获取所有配置的通知
        List<AlarmNotice> notices = service.GetConfigNotices(configIds);
        for (AlarmConfig config : configList) {
            try {
                //当前配置的规则
                List<AlarmRules> configRules = rules.stream().filter(r -> r.getAlarmId().equals(config.getId())).collect(Collectors.toList());
                for (AlarmRules rule : configRules) {
                    //校验规则
                    MonitorModel result = service.CheckRule(rule);
                    if (null != result && result.getWhetherTrigger()) {
                        //触发告警
                        List<AlarmNotice> configNotices = notices.stream().filter(r -> r.getAlarmId().equals(config.getId())).collect(Collectors.toList());
                        service.TriggerNotice(config, rule, configNotices, result);
                        break;
                    }
                }
            } catch (Exception e) {
                //记录当前异常信息
                throw new Exception(e);
            }
        }
    }

    //解除告警
    @Scheduled(cron = "0 0/5 * * * ?")
    @Async
    public void relieveAlarm() throws Exception {

        //获取所有告警配置
        List<AlarmRecord> recordList = service.GetAlarmRecord();
        //告警标识集合
        List<Integer> configIds = recordList.stream().map(AlarmRecord::getAlarmId).collect(Collectors.toList());
        if (configIds.size() == 0) return;
        //获取所有配置的规则
        List<AlarmRules> rules = service.GetConfigRules(configIds);
        for (AlarmRecord record : recordList) {
            try {
                //当前配置的规则
                List<AlarmRules> configRules = rules.stream().filter(r -> r.getAlarmId().equals(record.getAlarmId())).collect(Collectors.toList());

                //判断是否满足解除条件
                boolean relieve = configRules.stream().allMatch(rule -> {
                    try {
                        return service.CheckRelieve(rule, record);
                    } catch (Exception e) {
                        return false;
                    }
                });
                //解除告警
                if (relieve) service.RelieveAlarm(record);
            } catch (Exception e) {
                //记录当前异常信息
                throw new Exception(e);
            }
        }
    }
}
